from selenium import webdriver # 导入webdriver包
import time
from selenium.webdriver.chrome.options import Options

# 不加载图片,不缓存在硬盘(内存)
SERVICE_ARGS = ['--load-images=false', '--disk-cache=false']
chrome_options = Options()

#win平台专用
# chrome_options.add_argument('--headless')
# 创建浏览器, 添加参数设置为无界面浏览器
# driver = webdriver.Chrome(options=chrome_options)

# centos平台专用
# DRIVER_PATH = '/root/pythonProject/baijiu/chromedriver'   绝对路径
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-dev-shm-usage')
# driver = webdriver.Chrome(executable_path=DRIVER_PATH,options=chrome_options)


pageNumber = 999;

driver =webdriver.Chrome()#初始化一个chrome浏览器实例：driver

def init(qqNumber,qqPassword):
    print('初始化成功！')
    global driver
    driver.maximize_window()  # 最大化浏览器
    driver.get("https://www.qcc.com/")  # 通过get()方法，打开一个url站点
    gotoLogin(qqNumber,qqPassword)

def gotoIndex(realName):
    global driver
    print('解析页面',realName,'中')
    driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[4]/div/div[2]/div/table/tr[1]/td[3]/div/a').click();
    time.sleep(2)
    windows = driver.window_handles  ##在打开新页面的时候使用
    print('windows', windows)
    driver.switch_to.window(windows[1])  ##进入点击的那一页
    time.sleep(2)
    ##点击查看动态按钮
    driver.find_element_by_xpath('//span[@class="dt-tx"]/a').click();
    time.sleep(2)
    windows = driver.window_handles  ##在打开新页面的时候使用
    time.sleep(2)
    print('windows', windows)
    driver.switch_to.window(windows[2])  ##进入点击的那一页
    time.sleep(2)
    ##近一个月的数据
    # driver.find_element_by_xpath('//*[@id="filterApp"]/div[3]/div[2]/a[4]/span').click();
    ##近七天的数据
    # driver.find_element_by_xpath('//*[@id="filterApp"]/div[3]/div[2]/a[3]/span').click();
    # time.sleep(2)

    getNewList(realName);
    ##爬取结束 窗口关闭
    driver.close();
    driver.switch_to.window(windows[1])
    driver.close();
    driver.switch_to.window(windows[0])  # 跳到初始窗口

def clickFindMore(list):
    global  driver;
    for i in list:
        sm = i.find_elements_by_xpath('td[@class="tda-hight"]/div[@class="m-t-sm"]/a');
        if len(sm)!=0 :
            driver.execute_script("arguments[0].click();", sm[0]);
            # sm[0].click();
    time.sleep(2)
    return  driver.find_elements_by_xpath('//*[@id="dynamiclist"]/div/table/tbody/tr');

def getDataFromNewsList(newsList,realName):
        for i in newsList[1:]:
            level  = isEmpty(i.find_elements_by_xpath('td[1]/span'));
            content  = isEmpty(i.find_elements_by_xpath('td[3]/a'));
            kt = isEmpty(i.find_elements_by_xpath('td[@class="line-clamp"]'))
            cg = isEmpty(i.find_elements_by_xpath('td[@class="tda-hight"]'))
            type = isEmpty(i.find_elements_by_xpath('td[2]'))
            if kt != '暂无数据':
                content = kt
            if cg != '暂无数据':
                content = cg
            tip = isEmpty(i.find_elements_by_xpath('td[3]/div[@class="m-t-xs"]/span'));
            welcome = isWelcome(i.find_elements_by_xpath('td[@class="tda-hight"]/div[2]'));
            times = isEmpty(i.find_elements_by_xpath('td[4]'))
            localTime = time.strftime("%Y-%m-%d", time.localtime());
            if times.find('-') == -1:
                times = localTime;
            if level == "" and type == "" and content == "":
                print('空数据自动跳过！')
                continue;
            else:
                print('查询成功---', '--', realName, '--', level, '--', type, '--', content, '--', tip, '--', welcome, '--',times, '--', localTime)

        return True;
def getNewList(realName):
    global driver;
    global pageNumber;
    newsList = driver.find_elements_by_xpath('//*[@id="dynamiclist"]/div/table/tbody/tr');
    ##点击查看更多
    newsList = clickFindMore(newsList);
    ##获取底部页数 判断当前页码是不是符合 如果不符合则调过
    icon = driver.find_elements_by_xpath('//*[@id="dynamiclist"]/div/nav/ul/li');
    # 数据插入
    res = getDataFromNewsList(newsList, realName);
    ##如果返回false 说明已经找到上次更新的数据 非寻找页数的情况下
    if res == False :
        return;
    ##查看还有没有下一页
    if len(icon) > 1:
        number = 0
        if icon[len(icon) - 1].get_attribute('class')== 'active' :
            print('没有下一页！')
            return ;
        for i in range(0,len(icon)):
            if icon[i].get_attribute('class')=='active':
                print('当前第：',icon[i].find_elements_by_xpath('a')[0].text,'，页总共页数：',icon[len(icon)-1].find_elements_by_xpath('a')[0].text);

        for i in range (0,len(icon)):
            if icon[i].find_elements_by_xpath('a')[0].text == ">":
              icon[i].find_elements_by_xpath('a')[0].click();
              time.sleep(3)
              print('寻找到下一页！')
              getNewList(realName);
              return;
        return;
    else:
        print('没有下一页！')

def gotoLogin(qqNumber,qqPassword):
    print('开始登录！----登录前请先配置有绑定企查查的qq账号密码')
    global driver;
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/header/div/ul/li[10]/a').click();
    time.sleep(1)
    driver.find_element_by_xpath('//*[@id="verifyLoginPanel"]/div[2]/div/div/div[2]/a[2]').click();
    time.sleep(2)
    # 进入frame
    driver.switch_to.frame('ptlogin_iframe')
    driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click();
    driver.find_element_by_xpath('//*[@id="u"]').send_keys(qqNumber);
    driver.find_element_by_xpath('//*[@id="p"]').send_keys(qqPassword);
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="login_button"]').click();
    time.sleep(3)
    print('开始进行滑块验证！')
    driver.switch_to.frame('tcaptcha_iframe')
    hkYz();
    print('滑块验证成功！')
    print('qq登录成功！')
    time.sleep(3)
##滑块验证
def hkYz():
    global driver;
    x = 0;
    while len(driver.find_elements_by_xpath('//*[@id="tcaptcha_drag_thumb"]'))>0 :
        print('开始滚动')
        slider = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')
        # 鼠标点击并按住不松
        webdriver.ActionChains(driver).click_and_hold(slider).perform()
        # 让鼠标随机往下移动一段距离
        webdriver.ActionChains(driver).move_by_offset(xoffset=120, yoffset=0).perform()
        time.sleep(0.1)
        webdriver.ActionChains(driver).move_by_offset(xoffset=(40+x), yoffset=0).perform()
        webdriver.ActionChains(driver).release(slider).perform()
        x+=5;
        time.sleep(2)
        # 判断有没有出现登录风险按钮
        if len(driver.find_elements_by_xpath('//*[@id="ptlogin_iframe"]')) >0:
            driver.switch_to.frame('ptlogin_iframe')
            driver.find_element_by_xpath('//*[@id="loginform"]/div[4]/a').click();
            time.sleep(2)
            driver.switch_to.frame('tcaptcha_iframe')
            hkYz()
            time.sleep(2)

##在首页的时候输入关键字查询
def getData(dataList):
    global driver;
    print('进入搜索页面！');
    time.sleep(3)
    driver.find_element_by_xpath('//input[@id="searchkey"]').click();
    driver.find_element_by_xpath('//input[@id="searchkey"]').send_keys("北京三快科技有限公司");
    driver.find_element_by_xpath('//*[@id="indexSearchForm"]/div[1]/span/input').click();

    for i in dataList:
        realName = (str)(i);
        print('开始解析',realName)
        driver.find_element_by_xpath('//*[@id="searchKey"]').clear();
        driver.find_element_by_xpath('//*[@id="searchKey"]').send_keys(realName);
        driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div/div/div/div/span/button').click();
        # //前往页面
        gotoIndex(realName);

def isEmpty(array):
    if len(array) == 0:
        return '暂无数据'
    else:
        return ((str)(array[0].text)).replace('\n','').replace(' ','')

def isWelcome(array):
    if len(array) == 0:
        return '暂无数据'
    else:
        if ((str)(array[0].text)).find('来源')!= -1 :
            return ((str)(array[0].text)).replace('\n','').replace(' ','')
        else:
            return '暂无数据';
def start():
    global pageNumber
    ##到第几页爬取 默认为0
    pageNumber = 0
    try:
        ##验证登录
        qqNumber = '';  ##qq账号
        qqPassword = "";  ##qq密码
        init(qqNumber,qqPassword);
        ##遍历名称搜索
        dataList = ['贵州茅台', '北京三快科技有限公司'];
        getData(dataList);
        print('爬取结束！')
    except:
        print("未知异常")
    driver.quit();

# 爬虫启动命令 设置用main方法启动爬虫
if __name__ == '__main__':
    start();



